package tuding.android.bigplanettracks.ui;

import java.util.Map;
import java.util.Vector;

import tuding.android.bigplanettracks.BigPlanet;
import tuding.android.bigplanettracks.R;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class LauncherBar extends LinearLayout implements OnClickListener,DialogInterface.OnMultiChoiceClickListener {

    public static final String LAUNCHERBAR_KEY = "LAUNCHERBAR_ICONS";
    private Vector<int[]> m_tempButtonState = new Vector<int[]>();
    private int m_btnWidth = 48;
    private int m_btnHeight = 48;

    private ILauncherBarCallback m_callback;
    private Activity m_app;
    int[] idsStatic = { R.drawable.person,R.drawable.blogs32, R.drawable.start_track32,  R.drawable.images32, R.drawable.record };

    public LauncherBar(Context context) {
        super(context);
        __init();
        m_app= (Activity) context;
    }

    public LauncherBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        __init();
        m_app = (Activity)context;
    }

    public void setCallback(ILauncherBarCallback callback) {
        m_callback = callback;
    }

    public ILauncherBarCallback getCallback() {
        return m_callback;
    }

    private void __init() {
        LinearLayout.LayoutParams llParams;
        setGravity(Gravity.CENTER_VERTICAL);
        m_btnWidth = convertDensityPixel(m_btnWidth, getContext());
        m_btnHeight = convertDensityPixel(m_btnHeight, getContext());

        for (int i = 0; i < idsStatic.length; i++) {
            llParams = new LinearLayout.LayoutParams(m_btnWidth, m_btnHeight, 1);
            if(2 == i && BigPlanet.isGPSTracking == true) {
            	addView(__createButton(R.drawable.stop_track32, R.drawable.stop_track32), llParams);
            } else if(4 == i && BigPlanet.isAutoRecording == true) {
            	addView(__createButton(R.drawable.recording, R.drawable.recording), llParams);
            } else {
            	addView(__createButton(idsStatic[i], idsStatic[i]), llParams);
            }
        }

    }

    private LinearLayout __createButton(int buttonId, int imageId) {
        LayoutInflater inf = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout ll = (LinearLayout) inf.inflate(R.layout.launchbar_button,
                null);
 
        Button btn = (Button) ll.findViewById(R.id.launchbar_button_item);
        btn.setCompoundDrawablesWithIntrinsicBounds(0, imageId, 0, 0);
        ll.setBackgroundResource(R.drawable.launcherbar_selector);

        //btn.setText(text.replace(" ", "\u00A0"));
        ll.setId(buttonId);
        ll.setOnClickListener(this);
        // ll.setOnLongClickListener(this);

        return ll;
    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }

        int id = v.getId();
        Map<String, String> map = null;
        String desctitle;
        Drawable icon;
        Message msg = new Message();
        switch (id) {
        case R.drawable.start_track32:
        case R.drawable.stop_track32: 
        	msg.what = BigPlanet.Footer_Toggle_Track;
        	BigPlanet.footerHandler.sendMessage(msg);
            break;
        case  R.drawable.person:
        	msg.what = BigPlanet.Footer_Autofollow;
        	BigPlanet.footerHandler.sendMessage(msg);
        	break;
        case R.drawable.record:
        case R.drawable.recording:
        	Log.i("Recording", "pressed");
        	msg.what = BigPlanet.Footer_Toggle_Record;
        	BigPlanet.footerHandler.sendMessage(msg);
            break;
        case R.drawable.blogs32:
        	msg.what = BigPlanet.Footer_Add_notes;
        	BigPlanet.footerHandler.sendMessage(msg);
            break;

        case R.drawable.images32:
        	msg.what = BigPlanet.Footer_Add_photo;
        	BigPlanet.footerHandler.sendMessage(msg);
            break;

        // User added icons
        default:
            break;
        }
    }


    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        int save[] = { which, isChecked ? 1 : 0 };
        m_tempButtonState.add(save);
    }
    public static int convertDensityPixel(int dip, Context context) {
        return (int) (dip * context.getApplicationContext().getResources().getDisplayMetrics().density);
    }
}
